/*
 * @Description:
 * @Version: 1.0
 * @Autor: wangmeng
 * @Date: 2025-01-10 14:43:48
 * @LastEditors: wamgmeng
 * @LastEditTime: 2025-01-10 16:42:20
 */
export enum NavCmd {
    rotor = 1,
    rudder = 2, // 舵面
    sensor = 3,
    navigation = 4,
    control = 5,
    info = 6,
}

export interface INavParams {
    cmd: NavCmd;
    data: number;
}

export interface INavRotorParams extends INavParams {
    cmd: NavCmd.rotor;
}
export interface INavRudderParams extends INavParams {
    cmd: NavCmd.rudder;
}
export interface INavSensorParams extends INavParams {
    cmd: NavCmd.sensor;
}
export interface INavNavigationParams extends INavParams {
    cmd: NavCmd.navigation;
}
export interface INavControlParams extends INavParams {
    cmd: NavCmd.control;
}
export interface INavInfoParams extends INavParams {
    cmd: NavCmd.info;
}

export interface IRotorState {
    num: number;
    rotation_speed: number;
    pwm_fb: number;
    pwm_cmd: number;
    temp: number;
    status: number;
}

export interface IRotor {
    state_list: IRotorState[];
}

export class Rotor implements IRotor {
    state_list: IRotorState[] = [];

    constructor(data: IRotorState) {
        this.state_list.push(data);
    }

    static fromData(param: number) {
        return new Rotor({
            num: param & 0x07, // 取最低3位
            rotation_speed: (param >> 3) & 0x1f, // 取5位用于转速
            pwm_fb: (param >> 8) & 0xff, // 取8位用于PWM反馈
            pwm_cmd: (param >> 16) & 0xff, // 取8位用于PWM指令
            temp: (param >> 24) & 0xff, // 取8位用于温度
            status: (param >> 31) & 0x01, // 取最高位用于状态
        });
    }
}


// 1. 创建一个32位的数据包（通常从硬件或通信接口获得）
const rawData: number = 0b10101010_11110000_10101010_00000111;
// status  temp      pwm      speed+num

// 2. 使用静态方法创建Rotor实例
const rotor = Rotor.fromData(rawData);
console.log(rotor)

// 3. 访问电机状态信息
console.log('电机编号:', rotor.state_list[0].num);
console.log('电机转速:', rotor.state_list[0].rotation_speed);
console.log('PWM反馈值:', rotor.state_list[0].pwm_fb);
console.log('PWM命令值:', rotor.state_list[0].pwm_cmd);
console.log('电机温度:', rotor.state_list[0].temp);
console.log('电机状态:', rotor.state_list[0].status);







console.log(NavCmd)

console.log(NavCmd.rotor)